package com.zhao.modleclass.modle;

import android.util.Log;

import com.zhao.modleclass.bean.HomePageBean;
import com.zhao.modleclass.interfaces.IViewPager;
import com.zhao.modleclass.utils.Constant;

import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/11/27.
 */

public class Modle_HomePage implements IModle_HomePage {

    @Override
    public void loadHomepageData(Map map, final OnLoadHomepageDataListener listener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.URL_HOMEPAGE)
                //使用Rxjava与Retrofit配合一定要记得加这句话
                //否则不能创建Adapter 导致
                // IllegalArgumentException: Unable to create call adapter for rx.Observable
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IViewPager iViewPager = retrofit.create(IViewPager.class);
        Observable<HomePageBean> observable = iViewPager.getHomepageBean(map);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomePageBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(HomePageBean homePageBean) {
                    listener.onSuccess(homePageBean);
                    }
                });
    }
}
